package com.springx.examples.showcase.controller;

import com.google.common.collect.Maps;
import com.springx.examples.showcase.entity.Resource;
import com.springx.examples.showcase.service.ResourceService;
import com.springx.shiro.domain.Principal;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import com.springx.examples.showcase.common.spring.DateEditor;
import com.springx.examples.showcase.common.spring.HtmlCleanEditor;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * Created by roman_000 on 2015/7/12.
 */
public class BaseController {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    protected static final String PAGE_NUMBER = "_page";
    protected static final String PAGE_SIZE = "_pageSize";
    protected static final String OPERATE_TYPE = "operateType";
    protected static final String CRUMBS_ATTRIBUTE = "crumbs_attribute";

    @Autowired
    protected ResourceService resourceService;

    public PageRequest initPageRequest(HttpServletRequest request) throws ServletRequestBindingException {
        Integer pageNumber = ServletRequestUtils.getIntParameter(request, PAGE_NUMBER);
        Integer pageSize = ServletRequestUtils.getIntParameter(request, PAGE_SIZE);
        PageRequest pageRequest = new PageRequest(
                null == pageNumber ? 1 : pageNumber,
                null == pageSize ? 10 : pageSize
        );
        return pageRequest;
    }

    /**
     * 获取登录用户信息
     *
     * @return
     */
    public Principal getPrincipal() {
        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        return principal;
    }

    /**
     * 是否进行认证
     *
     * @return
     */
    public boolean isAuthenticated() {
        Subject subject = SecurityUtils.getSubject();
        return subject.isAuthenticated();
    }

    /**
     * 数据绑定
     *
     * @param binder WebDataBinder
     */
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new HtmlCleanEditor(true, true));
        binder.registerCustomEditor(Date.class, new DateEditor(true));
    }

    /**
     * 构建面包屑
     */
    public Map<String, String> buildCrumbsMap(String... menus) {
        Map<String, Resource> crumbsMap = resourceService.selectAllCrumbs();
        Map<String, String> result = Maps.newLinkedHashMap();
        String indexName = "首页";
        if (ArrayUtils.isNotEmpty(menus)) {
            Resource resource;
            for (String name : menus) {
                if (indexName.equals(name) || StringUtils.isBlank(name)) {
                    result.put(indexName, "/");
                } else {
                    resource=crumbsMap.get(name);
                    if(null!=resource) {
                        result.put(name, StringUtils.isBlank(resource.getUrl()) ? null : resource.getUrl() + "?mid=" + resource.getId());
                    }
                }

            }
        }
        return result;
    }
}
